package com.hfjyz.base.util;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Properties;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.model.PicturesTable;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import com.hfjyz.base.bean.BaseBean;

public final class WordUtil<T extends BaseBean> {
    public void readFromWord(InputStream template, InputStream inputDoc, T bean) throws Exception {
        Properties props = new Properties();
        props.load(template);

        Table table = getTable(inputDoc, bean);

        Method[] methods = bean.getClass().getMethods();
        if (methods == null || methods.length == 0) {
            return;
        }

        for (Method method : methods) {
            String name = method.getName();
            if (props.containsKey(name)) {
                String index = props.getProperty(name);
                String value = readFromTable(table, index);
                method.invoke(bean, value);
            }
        }
    }

    private static Table getTable(InputStream in, BaseBean bean) throws IOException {
        POIFSFileSystem pfs = new POIFSFileSystem(in);
        HWPFDocument hwpf = new HWPFDocument(pfs);
        // 得到文档的读取范围
        Range range = hwpf.getRange();
        TableIterator it = new TableIterator(range);
        if (it.hasNext()) {
            return it.next();
        }
        return null;
    }

    private static String readFromTable(Table table, String v) {
        String[] index = v.split(",");
        if (index.length < 2) {
            return null;
        }
        int row = Integer.valueOf(index[0]);
        int column = Integer.valueOf(index[1]);
        TableRow tr = table.getRow(row);
        TableCell td = tr.getCell(column);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < td.numParagraphs(); i++) {
            if (i != 0) {
                sb.append("\r\n");
            }
            Paragraph para = td.getParagraph(i);
            sb.append(para.text().trim());
        }
        return sb.toString();
    }

    public static byte[] readerImageFromWord(InputStream in) throws IOException {
        HWPFDocument doc = new HWPFDocument(in);
        PicturesTable picTab = doc.getPicturesTable();
        if (picTab == null) {
            return null;
        }
        List<Picture> picList = picTab.getAllPictures();
        if (picList == null || picList.isEmpty()) {
            return null;
        }
        Picture p = (Picture) picList.get(0);

        return p.getContent();
    }

    public void readRegDate(InputStream inputDOCtream, BaseBean bean) throws IOException {
        WordExtractor wordExtractor = new WordExtractor(inputDOCtream);
        String[] paragraph = wordExtractor.getParagraphText();
        if (paragraph != null) {
            for (String p : paragraph) {
                int index1 = p.indexOf("填表时间：");
                if (index1 > 0) {
                    bean.setRegDate(p.substring(index1 + 5, index1 + 15));
                }
                int index2 = p.indexOf("本人签字：");
                if (index2 > 0) {
                    bean.setSignValue(p.substring(index2 + 5));
                }
            }
        }
    }
}
